﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DonnetSpeech;
using System.Configuration;
using System.Data;
using FastReport;

namespace MINERP.FactoryGrid.COM
{
    public class AssignGrid
    {
        private static string strCon = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString;


        public static void AssignOrderGrid(string inputOrderCode,int gridid)
        {
            string inputBarCode = inputOrderCode.Trim();
            string[] input = inputBarCode.Split(new char[] { '-' });
            int orderProductID, subOrderID,outPutStatus;
            bool IsAllEnd = false;
        


            if (input.Length != 2)
            {
                MessageBox.Show("输入的条码有误，应符合:定单号-字单号,请重新输入!");
                return;
            }      
            if (!Int32.TryParse(input[0], out orderProductID) || !Int32.TryParse(input[1], out subOrderID))
            {
                MessageBox.Show("请输入正确的条码，条码里应全为数字!不能包含其它非数字");
                return;
            }
            using (SqlConnection con = new SqlConnection(strCon))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                var Command1 = con.CreateCommand();
                Command1.CommandText = "AssignFactoryIntoGrid";
                Command1.CommandType = CommandType.StoredProcedure;
                Command1.Parameters.Add(new SqlParameter("@orderProductID", orderProductID));
                Command1.Parameters.Add(new SqlParameter("@subOrderID", subOrderID));
                Command1.Parameters.Add(new SqlParameter("@GridID", gridid));
                Command1.Parameters.Add(new SqlParameter("@outPutStatus", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, "outPutStatus", DataRowVersion.Default, null));
                Command1.Parameters.Add(new SqlParameter("@isAllInGrid", SqlDbType.Bit, 0, ParameterDirection.Output, false, 0, 0, "isAllInGrid", DataRowVersion.Default, null));
                Command1.ExecuteNonQuery();
                IsAllEnd = Convert.ToBoolean(Command1.Parameters["@isAllInGrid"].Value);
                outPutStatus = Convert.ToInt32(Command1.Parameters["@outPutStatus"].Value);
            }
            if (outPutStatus == 0)
            {
                MessageBox.Show("没有找到拆分记录.请先分单");
                return;
            }
            else if (outPutStatus == -1)
            {
                MessageBox.Show("合册已经完成");
                return;
            }

            if (IsAllEnd)//所有子产品都在格子中了，询问是否需要打印出来，清格
            {
                if (MessageBox.Show("产品的所有部件都已完成，是否要打印清格", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //行清格后打印
                    SetOrerComplete(orderProductID);
                    PrintOrderInfo(orderProductID);
               
                }
            }

            MessageBox.Show("分配完成，请将产品放入格子中");
        }

        private static void SetOrerComplete(int orderProductID)
        {
            using (SqlConnection con = new SqlConnection(strCon))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                var Command1 = con.CreateCommand();
                Command1.CommandText = "SetOrderFactoryGridCompleted";
                Command1.CommandType = CommandType.StoredProcedure;
                Command1.Parameters.Add(new SqlParameter("@orderProductID", orderProductID));

                Command1.ExecuteNonQuery();
            }
        }

        public static void PrintOrderInfo(int OrderProductId)
        {
            using (SqlConnection con = new SqlConnection(strCon))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                var Command = con.CreateCommand();
                Command.CommandText = "GetFactoryGridPrintMsg";
                Command.CommandType = CommandType.StoredProcedure;
                Command.Parameters.Add(new SqlParameter ("@orderProductID",OrderProductId));
                SqlDataAdapter da = new SqlDataAdapter(Command);
                DataSet ds = new DataSet();
                da.Fill(ds, "Table");

                Report report = new Report();
                report.Load(Application.StartupPath + "\\Report\\车间合册小票.frx");
                report.RegisterData(ds, "Table");
                //report.Design();
                report.Print();
            }
        }
    }
}
